import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
declare var $: any
@Component({
  selector: 'sa-factory-config',
  templateUrl: './factory-config.component.html'
})
export class FactoryConfigComponent implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = '' //搜索keyword
  selectedCompanyName: string = '' //公司名称
  selectedCompanyId: string = '' //公司ID
  jobName: any = '' //任务名称
  updateUser: any = '' //编辑人
  typeName: string = '' //类型名称
  type: string = '' //类型ID
  threeStateText: boolean = false
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }
  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
  }
  ngOnInit() {
    this.load()
  }
  //刷新
  refresh() {
    this.load()
  }
  //表格搜索
  table_search() {
    this.curPage = 1
    this.searchKey = common.stringTrim(this.searchKey)
    this.load()
  }
  //初始化加载list数据
  load() {
    let tableSearch = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        searchKey: this.searchKey,
        companyName: this.selectedCompanyName,
        companyId: this.selectedCompanyId,
        typeName: this.typeName,
        type: this.type,
        jobName: this.jobName,
        updateUser: this.updateUser
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(tableSearch)
    this.jsonApiService.http('configuration/companyConfigurationFind.html', reqdata).subscribe(data => {
      this.tableData = data.exhibitDatas
      this.totalCount = data.recordCount
    })
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //删除
  delConfig(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('configuration/companyConfigurationDelete.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 修改
  editConfig(row, modal) {
    $('#factoryConfigForm').data('bootstrapValidator').resetForm()
    this.factoryForm.id = row.id
    this.companyArrays = row.companyInfo
    for (let key in this.factoryForm) {
      this.factoryForm[key] = row[key]
    }
    modal.show()
  }
  // 添加
  addFactoryConfig(modal) {
    modal.show()
    for (let key in this.factoryForm) {
      this.factoryForm[key] = ''
    }
    this.companyArrays = []
    $('#factoryConfigForm').data('bootstrapValidator').resetForm()
  }
  /**
   * 厂区配置模块==================================================================================
   */
  factoryForm: any = {
    id: '',
    jobName: '',
    typeName: '',
    type: '',
    companyId: '',
    companyName: ''
  }
  companyArrays: any[] = []
  validatorOptions = {
    excluded: [':disabled'],
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      jobName: {
        validators: {
          notEmpty: {
            message: '请输入任务名称'
          }
        }
      },
      type: {
        validators: {
          notEmpty: {
            message: '请输入类型ID'
          }
        }
      },
      typeName: {
        validators: {
          notEmpty: {
            message: '请输入类型名称'
          }
        }
      },
      companyName: {
        trigger: 'change',
        validators: {
          notEmpty: {
            message: '请选择公司'
          }
        }
      }
    }
  }
  // 厂区配置模块取消
  closeFactoryConfig(modal) {
    $('#factoryConfigForm').data('bootstrapValidator').resetForm()
    modal.hide()
  }
  addCompanySelected(data) {
    // companySelectedList
    this.factoryForm.companyId = data.companyIdSelectedList.join(',')
    this.factoryForm.companyName = data.companyNameSelectedList.join(',')
  }
  // 系数设置提交
  submitCoefConfig(modal) {
    $('#factoryConfigForm').bootstrapValidator('validate') //开启验证
    let isValid = $('#factoryConfigForm').data('bootstrapValidator').isValid() //验证是否通过
    const params = {
      ...this.factoryForm
    }
    if (params.id === '') delete params.id
    if (isValid) {
      this.jsonApiService.http_obj('configuration/companyConfigurationAdd.html', params).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
          modal.hide()
          $('#factoryConfigForm').data('bootstrapValidator').resetForm()
        }
      })
    }
  }
}
